#include <stdio.h>
#include <stdlib.h>

#define MAX 5
typedef char DataType;

typedef struct {

    DataType list[MAX];
    // 队列头部
    int front;
    // 队列尾部
    int rear;

} ArrayQueue;

ArrayQueue *init() {

    ArrayQueue *queue = (ArrayQueue *) malloc(sizeof(ArrayQueue));
    queue->rear = 0;
    queue->front = 0;

    return queue;
}

int isFull(ArrayQueue *queue) {
    return (queue->rear + 1) % MAX == queue->front;
}

int isEmpty(ArrayQueue *queue) {
    return queue->front == queue->rear;
}


void push(ArrayQueue *queue, DataType data) {

    if (isFull(queue)) {
        printf("queue is full !\n");
        return;
    }
    queue->list[queue->rear] = data;
    queue->rear = (queue->rear + 1) % MAX;
}

DataType pop(ArrayQueue *queue) {

    if (isEmpty(queue)) {
        return '0';
    }

    DataType data = queue->list[queue->front];
    queue->front = (queue->front + 1) % MAX;

    printf("pop value=%c\n", data);

    return data;
}

void toString(ArrayQueue *queue) {

    printf("[");
    int start = queue->front;

    while (start != queue->rear) {
        printf("%c,", queue->list[start]);
        start = (start + 1) % MAX;
    }
    printf("]\n");

}


int main() {
    printf("Hello, World!\n");

    ArrayQueue *queue = init();

    push(queue, 'a');
    push(queue, 'b');
    push(queue, 'c');
    push(queue, 'd');

    toString(queue);

    pop(queue);
    pop(queue);
    pop(queue);
    toString(queue);

    push(queue, 'd');
    push(queue, 'e');
    push(queue, 'f');
    toString(queue);

    return 0;
}
